Skip to content

fix: track origins by client map key in Agent teardown#5537

Open
emmayusufu wants to merge 1 commit into
nodejs:mainfrom
emmayusufu:fix/agent-teardown-kurl
Open

fix: track origins by client map key in Agent teardown#5537
emmayusufu wants to merge 1 commit into
nodejs:mainfrom
emmayusufu:fix/agent-teardown-kurl

Conversation

@emmayusufu

Copy link
Copy Markdown

This relates to...

Fixes #5529

Rationale

Agent#closeClientIfUnused reads client[kUrl].origin for every dispatcher in the client map, but the factory option is public API and nothing guarantees a factory-built dispatcher sets kUrl. ProxyAgent's own factory registers Http1ProxyWrapper (and Socks5ProxyAgent), neither of which does, so since 8.7.0 a plain-http request through a proxy crashes with Cannot read properties of undefined (reading 'origin') on the disconnect / connectionError teardown path.

Changes

The teardown closure already holds the origin string that was added to kOrigins and the map key derived from it, so the bookkeeping doesn't need kUrl at all: check the remaining map keys for the origin (including the #http1-only variant) and delete the captured origin string. This is the third option suggested in the issue.

A side effect worth noting: kOrigins.delete is now symmetric with the add. Previously add used String(opts.origin) while delete used kUrl.origin, which diverge when opts.origin is a URL instance (trailing slash), so those origins were never removed and counted against maxOrigins forever.

The stats getter still reads dispatcher[kUrl].origin and has the same fragility with kUrl-less dispatchers; left alone here since changing its keys is a visible behavior change. Can follow up if there's interest.

Features

N/A

Bug Fixes

  • ProxyAgent no longer crashes the inner Agent on connection teardown for plain-http (non-tunneled) targets.

Breaking Changes and Deprecations

N/A

Status

closeClientIfUnused read client[kUrl].origin for every dispatcher in the
client map, but dispatchers produced by a custom factory, including the
Http1ProxyWrapper and Socks5ProxyAgent that ProxyAgent registers, never
set kUrl, so plain-http requests through a proxy crashed on the
disconnect and connectionError teardown path. Compare the map keys
against the origin string captured at dispatch time instead, which also
makes the kOrigins delete symmetric with the add when opts.origin is a
URL instance.

Signed-off-by: Emmanuel Yusufu Kimaswa <kimaswaemma36@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ProxyAgent: "Cannot read properties of undefined (reading 'origin')" in Agent#closeClientIfUnused for HTTP targets (regression in 8.7.0)

1 participant